package interviewClassic150;

import org.junit.Test;

public class IntToRoman {
    public String intToRoman(int num) {
        //定义String和int数组，映射关系
        int[] nums = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
        String[] roman = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

        //num依次减去其能减的最大值，将对应的字符串拼接上去
        StringBuilder sb = new StringBuilder();
        while (num > 0) {
            for (int i = 0; i < nums.length; i++) {
                if (num >= nums[i]) {
                    num -= nums[i];
                    sb.append(roman[i]);
                    break;
                }
            }
        }

        return sb.toString();
    }

    @Test
    public void test() {
        int num = 3749;
        String s = intToRoman(num);
        System.out.println(s);
    }
}
